<!DOCTYPE html>
<html style="width:100%;height:100%;">
<head lang="en">
    <meta charset="UTF-8">
    <title>权限列表</title>
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/default/easyui.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/color.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/icon.css">
    <link type="text/css" rel="stylesheet" href="/resources/css/yidong.css">
    <script type="text/javascript" src="/resources/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/resources/easyui/plugin/jquery.cookie.js"></script>
    <script type="text/javascript" src="/resources/js/Util.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            loadRoleList();
        });

        function loadRoleList(successFun){
            restClient('/rest/role/list', function(data){
                $('#roleList').datagrid('loadData',data);
                if(successFun) successFun();
            });
        }
    </script>
</head>
<body onload="isLogon();" style="width:100%;height:100%;margin: 0;">
<table id="roleList" class="easyui-datagrid" style="width:100%;height:100%;"
       toolbar="#toolbar" rownumbers="true" fitColumns="true" singleSelect="true">
    <thead>
    <tr>
        <th field="name" width="50">角色名</th>
        <th field="description" width="200">描述</th>
        <th field="status" width="20" data-options="formatter:formatStatus">状态</th>
    </tr>
    </thead>
</table>
<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newOne()">新建</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editOne()">编辑</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyOne()">删除</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-lock" plain="true" onclick="authorize()">权限</a>
</div>

<div id="dlg" class="easyui-dialog" style="width:400px;height:auto;padding:-20px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <div class="fitem">
            <label>角色名：</label>
            <input id="name" name="name" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>描述：</label>
            <input id="description" name="description" class="easyui-textbox">
        </div>
        <div class="fitem">
            <label>状态：</label>
            <select id="status" name="status" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">
                <option value="0">启用</option>
                <option value="-1">禁用</option>
                <option value="-2">删除</option>
            </select>
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveOne()" style="width:90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
</div>

<div id="dlg-auth" class="easyui-dialog" style="width:400px;height:350px;padding:-20px"
     closed="true" buttons="#dlg-auth-buttons">
    <ul id="tt-auth" class="easyui-tree" data-options="animate:true,checkbox:true,onlyLeafCheck:true"></ul>
</div>
<div id="dlg-auth-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveAuth()" style="width:90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg-auth').dialog('close')" style="width:90px">取消</a>
</div>

<script type="text/javascript">
    var url;
    var successMsg;
    function newOne(){
        $('#dlg').dialog('open').dialog('setTitle','新建');
        $('#fm').form('clear');
        $('#status').combobox('setValue', 0);
        url = '/rest/role/create';
        successMsg = "添加成功";
    }
    function editOne(){
        var row = $('#roleList').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('open').dialog('setTitle', '编辑');
            $('#fm').form('load',row);
            url = '/rest/role/update/'+row.id;
            successMsg = "编辑成功";
        }
    }
    function saveOne(){
        restClient(url, function (result) {
            if (result.success) {
                $('#dlg').dialog('close');
                loadRoleList(function () {
                    $.messager.show({
                        title: '温馨提醒',
                        msg: successMsg
                    });
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: result.msg ? result.msg : result.message
                });
            }
        }, $('#fm').serialize());
    }
    function destroyOne(){
        var row = $('#roleList').datagrid('getSelected');
        if (row){
            $.messager.confirm('确认','确认要删除【'+row.name+'】么?',function(r){
                if (r){
                    restClient('/rest/role/delete/'+row.id, function(result){
                        if (result.success){
                            loadRoleList(function(){
                                $.messager.show({
                                    title: '温馨提醒',
                                    msg: '删除成功'
                                });
                            });
                        } else {
                            $.messager.show({
                                title: '温馨提醒',
                                msg: result.msg ? result.msg : result.message
                            });
                        }
                    });
                }
            });
        }
    }

    var roleId;
    function authorize(){
        var row = $('#roleList').datagrid('getSelected');
        if (row){
            roleId = row.id;
            $('#dlg-auth').dialog('open').dialog('setTitle','权限管理');
            restClient('/rest/menu/list', function(data){
                $('#tt-auth').tree('loadData', data);
                restClient('/rest/rolemenu/list/'+roleId, function(result){
                    if(result.success) {
                        var roleMenuData = result.data;
                        for (var index in roleMenuData) {
                            var node = $('#tt-auth').tree('find', roleMenuData[index].menuId);
                            $('#tt-auth').tree('check', node.target);
                        }
                    } else {
                        $.messager.show({
                            title: '温馨提醒',
                            msg: result.msg ? result.msg : result.message
                        });
                    }
                })
            });
        }
    }
    function saveAuth(){
        var nodes = $('#tt-auth').tree('getChecked');
        var menuIds = '';
        for(var index in nodes) {
            menuIds += nodes[index].id + ",";
        }
        restClient('rest/rolemenu/insert/'+roleId, function(result){
            $('#dlg-auth').dialog('close');
            if(result.success){
                $.messager.show({
                    title: '温馨提醒',
                    msg: '保存成功'
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: result.msg ? result.msg : result.message
                });
            }
        }, {menuIds:menuIds});
    }
</script>
</body>
</html>